package com.future.mayi.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.activity.NewGoodsBatchOrderActivity;
import com.future.mayi.app.App;
import com.future.mayi.model.GoodsBatchModel;
import com.future.mayi.model.MultiPostfeeModel;
import com.future.mayi.model.SpecModel;
import com.future.mayi.model.interfaces.GoodsBatchItemListener;
import com.future.mayi.response.MultiPostfeeResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.utils.PriceUtils;
import com.future.mayi.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoodsBatchOrderItemView extends LinearLayout implements View.OnClickListener {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.fl_goods)
    FlowLayout flGoods;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_postage)
    TextView tvPostage;
    @BindView(R.id.tv_note)
    TextView tvNote;
    @BindView(R.id.tv_back_price)
    TextView tvBackPrice;


    private GoodsBatchModel model;
    private int position;

    private LayoutInflater mInflater;


    private Context mContext;

    public GoodsBatchOrderItemView(Context context) {
        this(context, null);
    }

    public GoodsBatchOrderItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GoodsBatchOrderItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        View.inflate(context, R.layout.layout_goods_batch_order_item, this);
        ButterKnife.bind(this);
        mInflater = LayoutInflater.from(context);
    }

    @OnClick({R.id.tv_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_delete:
                if (mContext instanceof NewGoodsBatchOrderActivity) {
                    Intent intent = new Intent();
                    intent.putExtra("position", position);
                    ((NewGoodsBatchOrderActivity) mContext).setResult(Activity.RESULT_OK, intent);
                    ((NewGoodsBatchOrderActivity) mContext).finish();
                }
                break;
        }
    }


    public List<SpecModel> getSelectList() {
        return model == null ? null : model.getSelectList();
    }

    public void setModel(GoodsBatchModel model, int position) {
        this.position = position;
        tvTitle.setText("订单" + (position + 1));
        if (this.model == model) return;
        this.model = model;
        tvAddress.setText(model.getAddress() == null ? "" : model.getAddress());
        tvNote.setText(model.getMemo() == null ? "无" : model.getMemo());
        if (model.getPostage() == -1) {
            tvPostage.setText("计算中...");
            getMultiPostfee(model.getGoods_id(), model.getOrder_info());
        } else {
            tvPostage.setText("¥" + PriceUtils.getPriceText2(model.getPostage()));
        }
        flGoods.removeAllViews();
        double rebate_price = 0;
        if (getSelectList() != null && getSelectList().size() > 0) {
            for (SpecModel specModel : model.getSelectList()) {
                View view = mInflater.inflate(R.layout.layout_batch_order_goods_item_view, flGoods, false);
                TextView tvName = view.findViewById(R.id.tv_name);
                TextView tvPrice = view.findViewById(R.id.tv_price);
                TextView tvNum = view.findViewById(R.id.tv_num);
                View viewLine = view.findViewById(R.id.view_line);
                tvName.setText(specModel.getGoods_spec());
                tvPrice.setText(PriceUtils.getPriceText2(specModel.getGoods_price()));
                viewLine.setVisibility(VISIBLE);
                tvNum.setVisibility(VISIBLE);
                tvNum.setText("*" + specModel.getNum());
                rebate_price = rebate_price + PriceUtils.getMultiply(specModel.getNum(), specModel.getRebate_price());
                flGoods.addView(view);
            }
        }
        tvPrice.setText(PriceUtils.getPriceText2(model.getMoney() + model.getPostage()));
        tvBackPrice.setText(PriceUtils.getPriceText2(rebate_price));
    }


    private void getMultiPostfee(String goods_id, String order_info) {
        new HttpRequest<MultiPostfeeResponse>()
                .with(mContext)
                .setApiCode(ApiCst.multiPostfeeGoods)
                .addParam("goods_id", goods_id)
                .addParam("order_info", order_info)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<MultiPostfeeResponse>() {

                    @Override
                    public void onSuccess(MultiPostfeeResponse response) {
                        MultiPostfeeModel multiPostfeeModel = response.model;
                        if (multiPostfeeModel == null) return;
                        model.setPostage(multiPostfeeModel.getPost_fee());
                        tvPostage.setText("¥" + PriceUtils.getPriceText2(model.getPostage()));
                    }

                    @Override
                    public void onFail(String message, MultiPostfeeResponse response) {
                        HCUtils.loadFail(mContext, message);
                    }
                }).start(new MultiPostfeeResponse());

    }

}
